/*
 * @Descripttion:
 * @version: 1.0
 * @Author: 张泽涛
 * @LastEditors: 张泽涛
 * @LastEditTime: 2021-02-24 19:46:58
 */
import Link from './components/link'
import View from './components/view'
export let _Vue = null
export default function install (Vue) {
  // 防止重复注册
  if (install.installed && _Vue === Vue) return
  install.installed = true
  _Vue = Vue
  _Vue.mixin({
    beforeCreate () {
      // this 此处的就是vue的实例
      if (this.$options.router) {
        // 当前的this是否是 Vue 的根实例
        this._router = this.$options.router
        this._router.init(this)
        this._routerRoot = this
        Vue.util.defineReactive(this, '_route', this._router.history.current)
      } else {
        this._routerRoot = this.$parent && this.$parent._routerRoot
      }
    }
  })

  Object.defineProperty(Vue.prototype, '$router', {
    get () { return this._routerRoot._router }
  })

  Object.defineProperty(Vue.prototype, '$route', {
    get () { return this._routerRoot._route }
  })

  _Vue.component(Link.name, Link)
  _Vue.component(View.name, View)
}
